package com.example.test.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.test.ActivityControler;
import com.example.test.AddCardActivity;
import com.example.test.R;
import com.example.test.demin.Card;
import com.example.test.demin.User;
import com.example.test.fragment.CardFragment.ViewHolder;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class IncreaseFragment extends BaseFragment implements OnClickListener {

	@ViewInject(R.id.choose_card)
	private LinearLayout choose_card;
	@ViewInject(R.id.et_money)
	private EditText et_money;
	@ViewInject(R.id.btn_increase)
	private Button btn_increase;
	@ViewInject(R.id.ll_increase)
	private LinearLayout ll_increase;
	@ViewInject(R.id.ll_checkcard)
	private LinearLayout ll_checkcard;
	@ViewInject(R.id.tv_bank_name)
	private TextView tv_bankname;
	@ViewInject(R.id.ly_card)
	private LinearLayout ly_card;
	@ViewInject(R.id.tv_card_number)
	private TextView tv_cardnumber;
	@ViewInject(R.id.tv_addcard)
	private TextView tv_addcard;
	List<Card> list = new ArrayList<Card>();
	private PopupWindow popwin;
	private ListView listview;
	private DbDaoUtils dao;
	private String money;
	private String username;
	private double doublemoney;
	private User user;
	private Card card;

	@Override
	public void initData() {
		dao = new DbDaoUtils(ct);
		list = dao.findAllCard();
		if (list.isEmpty()) {
			tv_addcard.setVisibility(View.VISIBLE);
			choose_card.setVisibility(View.GONE);
			tv_addcard.setOnClickListener(this);
		} else {
			tv_bankname.setText(list.get(0).getBankname());
			tv_cardnumber.setText(list.get(0).getCardnumber());
			ly_card.setBackgroundResource(ids2[0]);
			choose_card.setOnClickListener(this);
			btn_increase.setOnClickListener(this);
			inintlistview();
		}
	}

	private void inintlistview() {
		// TODO Auto-generated method stub
		listview = new ListView(ct);
		CardListAdapter adapter = new CardListAdapter();
		listview.setAdapter(adapter);
		listview.setVerticalScrollBarEnabled(false);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				tv_bankname.setText(list.get(position).getBankname());
				tv_cardnumber.setText(list.get(position).getCardnumber());
				ly_card.setBackgroundResource(ids2[position]);
				popwin.dismiss();
			}
		});
	}

	@Override
	public View initView(LayoutInflater inflater) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.increase_layout, null);
		ViewUtils.inject(this, view);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_addcard:
			Intent intent=new Intent(ct, AddCardActivity.class);
			startActivity(intent);
		    break;
		case R.id.choose_card:
			popwin = new PopupWindow(ct);
			popwin.setWidth(choose_card.getWidth());
			popwin.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
			
			popwin.setFocusable(true);
			popwin.setContentView(listview);
			popwin.setOutsideTouchable(true);
			popwin.showAsDropDown(choose_card, 0, 0);

			break;

		case R.id.btn_increase:
			if (checkForm()) {
				sp = ct.getSharedPreferences("user", Context.MODE_PRIVATE);
				username = sp.getString("username", "");
				if (TextUtils.isEmpty(username)) {
					Toast.makeText(ct, "请重新登陆，登陆已过期", 0).show();
				} else {
					doublemoney = Double.parseDouble(money);
					user = dao.findUserInfo(username);
					card = dao.findCardInfo(tv_cardnumber.getText().toString());
					if (card.getMoney() >= doublemoney) {
						double usermoney = user.getMoney() + doublemoney;
						double cardmoney = card.getMoney() - doublemoney;
						dao.updateCardMoney(tv_cardnumber.getText().toString(), cardmoney);
						dao.updateMoney(username, usermoney);
						Toast.makeText(ct, "充值成功", 0).show();
						ActivityControler.finishAll();
					} else {
						Toast.makeText(ct, "充值失败", 0).show();
					}
				}

			}
			break;
		default:
			break;
		}
	}

	private boolean checkForm() {
		// TODO Auto-generated method stub
		money = et_money.getEditableText().toString().trim();
		if (TextUtils.isEmpty(money)) {
			Toast.makeText(ct, "充值金额不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private int[] ids2 = { R.color.gridcolor1, R.color.gridcolor2,
			R.color.gridcolor3, R.color.gridcolor4, R.color.gridcolor5,
			R.color.gridcolor6 };
	
	class CardListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = View.inflate(ct, R.layout.carditem_layout, null);
				holder = new ViewHolder();
				holder.tv_bankname = (TextView) convertView
						.findViewById(R.id.tv_bank_name);
				holder.tv_cardnumber = (TextView) convertView
						.findViewById(R.id.tv_card_number);
				holder.ly_card=(LinearLayout) convertView.findViewById(R.id.ly_card);
				convertView.setTag(holder);
			}
			holder.tv_bankname.setText(list.get(position).getBankname());
			holder.tv_cardnumber.setText(list.get(position).getCardnumber());
			int i=position%ids2.length;
		    if(i>=6){
		    	i=6;
		    }
			holder.ly_card.setBackgroundResource(ids2[i]);
			return convertView;
		}

	}

	public static class ViewHolder {
		TextView tv_cardnumber;
		TextView tv_bankname;
		LinearLayout ly_card;
	}

}
